﻿<components:ExtListBox x:Class="Mnk.Library.WpfControls.Components.ButtonsView.GroupedList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                       xmlns:buttonsView="clr-namespace:Mnk.Library.WpfControls.Components.ButtonsView"
                       xmlns:components="clr-namespace:Mnk.Library.WpfControls.Components"
                       mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" ScrollViewer.CanContentScroll="False" ItemsSource="{Binding Items}" AlternationCount="1" x:Name="root">
    <ListBox.Resources>
        <buttonsView:RowColorConverter x:Key="RowColorConverter" />
    </ListBox.Resources>
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="buttonsView:IButtonInfo">
            <Button Width="{Binding CellSize, ElementName=root}" Height="{Binding CellSize, ElementName=root}" Click="ClickItem" VerticalAlignment="Top" VerticalContentAlignment="Top" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" KeyUp="KeyUpItem" Margin="0" Padding="0">
                <StackPanel Margin="3">
                    <Image Source="{Binding Icon}" HorizontalAlignment="Center" Width="{Binding IconSize, ElementName=root}" Height="{Binding IconSize, ElementName=root}" />
                    <TextBlock Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" TextAlignment="Center" />
                </StackPanel>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.GroupStyle>
        <GroupStyle AlternationCount="2" >
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander IsExpanded="True" Background="{Binding RelativeSource={RelativeSource AncestorType=GroupItem}, Path=(ItemsControl.AlternationIndex), Converter={StaticResource RowColorConverter}}" Margin="0" Padding="2">
                                    <Expander.Header>
                                        <DockPanel>
                                            <TextBlock Text="{Binding Path=Items[0].GroupName}" Margin="5,0,0,0" HorizontalAlignment="Stretch" FontWeight="Bold"/>
                                        </DockPanel>
                                    </Expander.Header>
                                    <Expander.Content >
                                        <ItemsPresenter />
                                    </Expander.Content>
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ItemsControl.GroupStyle>
</components:ExtListBox>
